package com.example.czxyindex.web;

import com.example.czxyindex.web.remote.IAdvaAction;
import com.example.czxyindex.web.remote.IProductAction;
import com.github.pagehelper.PageHelper;
import com.yc.czxy.bean.*;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class IndexAction {

    @GetMapping({"index","/"})
    public String index() {
        return "index";
    }



    /**
     * 用于打开template下指定页面
     * @return
     */
    @GetMapping("to{page}")
    public String topage(@PathVariable String page){
        return page;
    }

    /**
     * 商品操作
     */
    @Resource
    IProductAction iProductAction;

    /**
     * 广告接口
     */
    @Resource
    IAdvaAction iAdvaAction;



    /**
     * 添加标签
     *
     * @param label
     * @return
     */
    @PostMapping("addLabel")
    public Result<Label1> addLabel(Label1 label) {
        System.out.println(label.toString());
        return iProductAction.addLabel(label);
    }

    @GetMapping({ "goods", "goods.html"})
    public ModelAndView index(ModelAndView modelAndView) {
        modelAndView.setViewName("goods");
        List<Product> product = iProductAction.queryAll();
        modelAndView.addObject("plist", product);
        //查询要显示的广告
        PageHelper.startPage(1,3);
        List<Adva> alist =iAdvaAction.queryAdva();
        modelAndView.addObject("alist",alist);
        iAdvaAction.updateSchedule();
        return modelAndView;
    }

    @GetMapping({"goods_detail","goods_detail.html"})
    @ResponseBody
    public  ModelAndView goods(Integer pid,ModelAndView modelAndView) {
        modelAndView.setViewName("goods_detail");
        List<Product> product1 = iProductAction.queryByPid(pid);
        System.out.println(product1);
        modelAndView.addObject("plist", product1);
        return modelAndView;
    }
}
